package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.MyWalletResponse;
import com.huizhonglm.diap.api.model.user.OrderInfo;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.user.OrderListPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.user.iView.OrderListIView;
import com.huizhonglm.diap.ui.wallet.PaymentActivity;
import com.huizhonglm.diap.widget.LoadView;
import com.bruce.pullrefresh.PullToRefreshBase;
import com.bruce.pullrefresh.PullToRefreshListView;

import java.util.List;

/**
 * 订单列表
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class OrderListActivity extends BaseActivity implements OrderListIView,
        AdapterView.OnItemClickListener, LoadView.OnLoadAgainListener,
        PullToRefreshBase.OnRefreshListener2<ListView>, OrderListAdapter.OnPaymentListener {

    private PullToRefreshListView pullView;
    private ListView mListView;
    private LoadView loadView;

    private List<OrderInfo> mData;
    private OrderListAdapter mAdapter;
    private OrderListPresenter mPresenter;

    private String userID;
    private String orderType;

    private OrderInfo payOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        orderType = getIntent().getStringExtra("orderType");
        mPresenter = new OrderListPresenter(this);
        assignViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        onLoadAgain();
    }

    @Override
    protected void assignViews() {
        loadView = (LoadView) findViewById(R.id.load_view);
        pullView = (PullToRefreshListView) findViewById(R.id.orders_list);
        pullView.setMode(PullToRefreshBase.Mode.BOTH);
        pullView.setOnRefreshListener(this);
        mListView = pullView.getRefreshableView();
        mListView.setOnItemClickListener(this);
        loadView.setDataView(pullView, this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onPayment(OrderInfo order) {
        payOrder = order;
        showProgressBar();
        mPresenter.loadMyWallet();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mPresenter.loadData(Constants.LOAD_DROP, userID, orderType);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        mPresenter.loadData(Constants.LOAD_MORE, userID, orderType);
    }

    @Override
    public void onLoadAgain() {
        mPresenter.loadData(Constants.LOAD_INIT, userID, orderType);
    }

    @Override
    public void onLoadDataSuccess(String pullFlag, List<OrderInfo> data) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                this.mData = data;
                updatePage(data);
                loadView.showDataView();
                break;
            case Constants.LOAD_DROP:
                this.mData = data;
                updatePage(data);
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                this.mData.addAll(data);
                updatePage(data);
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadDataFailure(String pullFlag) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                loadView.showErrorView();
                break;
            case Constants.LOAD_DROP:
                ToastUtil.makeTextShort("刷新失败");
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                ToastUtil.makeTextShort("加载更多失败");
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadDataEmpty(String pullFlag) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                loadView.showEmptyView(null);
                break;
            case Constants.LOAD_DROP:
                ToastUtil.makeTextShort("列表已经是最新");
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                ToastUtil.makeTextShort("到底部啦");
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadMyWalletSuccess(MyWalletResponse resp) {
        hideProgressBar();
        if (resp != null) {
            Intent payPage = new Intent(this, PaymentActivity.class);
            payPage.putExtra("wallet", resp);
            payPage.putExtra("order", payOrder);
            startActivity(payPage);
        } else {
            ToastUtil.makeTextShort("初始化付款失败，请稍后重试");
        }
    }

    @Override
    public void onLoadMyWalletFailure() {
        hideProgressBar();
        ToastUtil.makeTextShort("初始化付款失败，请稍后重试");
    }

    private void updatePage(List<OrderInfo> data){
        setPullViewMode(data);
        if (mAdapter == null) {
            mAdapter = new OrderListAdapter(getLayoutInflater(), mData);
            mAdapter.setOnPaymentListener(this);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.setData(mData);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void setPullViewMode(List<OrderInfo> data){
        if (!EmptyUtil.isEmpty(data)) {
            if (mData.size() >= Constants.PAGE_SIZE) {
                if (data.size() < Constants.PAGE_SIZE) {
                    pullView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    pullView.setMode(PullToRefreshBase.Mode.BOTH);
                }
            } else {
                pullView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
        }
    }
}
